package chances.epg.data.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import chances.epg.api.controller.AbstractDataController;
import chances.epg.commons.utils.DateUtils;
import chances.epg.data.PageBean;
import chances.epg.data.model.CategoryItemProxy;
import chances.epg.entity.content.CategoryItem;
import chances.epg.entity.content.Episode;
import chances.epg.entity.content.Series;

@Service
public class CategoryItemProxyService {
	
	/*@Autowired
	protected SqlSessionTemplate epgDBClient;*/
	
	@Autowired
	protected CategoryService categoryService;
	
	@Autowired
	protected ContentService contentService;

	public List<CategoryItemProxy> listCategoryItem(List<CategoryItem> items, String categoryCode, String itemType,
			int start, PageBean pageBean) {
		List<CategoryItemProxy> result = new ArrayList<CategoryItemProxy>();
		//List<CategoryItem> items = categoryService.listCategoryItem(categoryCode, itemType, start, pageBean);
		for (CategoryItem item : items) {
			CategoryItemProxy categoryItemProxy = new CategoryItemProxy(item);
			if (CategoryItem.ITEMTYPE_SERIES.equals(item.getItemType())
					|| CategoryItem.ITEMTYPE_SERIES2.equals(item.getItemType())) {
				Series series = this.contentService.getSeriesByCode(item.getItemCode());
				List<Episode> episodes = contentService.getEpisodes(item.getItemCode(),item.getItemType());
				categoryItemProxy.setFinished(false);
				categoryItemProxy.setUpdateNum("0");
				categoryItemProxy.setChildrenTitle("");
				if (episodes.size() > 0) {
					if(series.getSeriesType().equals(CategoryItem.ITEMTYPE_SERIES)){
						categoryItemProxy.setFinished(series.getEpisodeNumber() == episodes.get(episodes.size()-1).getEpisodeIndex());
						categoryItemProxy.setUpdateNum(""+episodes.get(episodes.size()-1).getEpisodeIndex());
					}else if(series.getSeriesType().equals(CategoryItem.ITEMTYPE_SERIES2)){
						categoryItemProxy.setChildrenTitle(episodes.get(0).getContentTitle());
						//系列剧使用MM-dd作为更新期数
						if(null != episodes.get(0).getIssueDate()){
							categoryItemProxy.setUpdateNum(DateUtils.formatDateMMdd(episodes.get(0).getIssueDate()));
						}
						//系列剧使用横图
						categoryItemProxy.setStill(series.getStill());
					}
				}
			}
			result.add(categoryItemProxy);
		}
		return result;
	}

	/*public List<CategoryItemProxy> getCategoryItems(Map<String, Object> paramsMap, boolean byParent) {
		return categoryService.getCategoryItems(paramsMap, byParent);
	}*/
	
	
	
	
}
